package com.realtyManage.springboot.Controller;

import com.alibaba.fastjson.JSON;
import com.realtyManage.springboot.Config.Wxacode;
import com.realtyManage.springboot.Dao.CommodityRepository;
import com.realtyManage.springboot.Dao.ExchangeRepository;
import com.realtyManage.springboot.Dao.UserRepository;
import com.realtyManage.springboot.Model.Do.*;
import com.realtyManage.springboot.Model.Vo.BaseEntity;
import com.realtyManage.springboot.Model.Vo.CouponEntity;
import com.realtyManage.springboot.Model.Vo.SubordinateEntity;
import com.realtyManage.springboot.Service.AsyncService;
import com.realtyManage.springboot.Service.CommodityService;
import com.realtyManage.springboot.Service.IUserService;
import com.realtyManage.springboot.Utils.IdGen;
import com.realtyManage.springboot.Utils.SysExecRsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * CommodityController
 * 积分商城
 * @Author 杭州素色网络科技有限公司
 * @Date 2019/12/13
 **/

@Controller
@RequestMapping("/")
public class CommodityController {
    @Autowired
    private CommodityService commodityService;

    @Autowired
    private IUserService userService;

    @Autowired
    private AsyncService asyncService;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private CommodityRepository commodityRepository;

    @Autowired
    private ExchangeRepository exchangeRepository;

    @Value("${image.root.path}")
    private String imageRootPath;

    @Value("${wx.appid}")
    private String appid;

    @Value("${wx.appsecret}")
    private String secret;

    @Value("${real.path}")
    private String realPath;


    /**
     * 新增商品
     * @param commodity
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addCommodity",method = RequestMethod.POST)
    private String addCommodity(Commodity commodity, HttpServletRequest request){
        Integer code = -1;
        commodity.setState(0);
        commodity.setNum("0");
        if (commodityService.addCommodity(commodity) > 0){
            code = 1;
        }
        BaseEntity baseEntity =new BaseEntity();
        baseEntity.setInfoCode(code);
        baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return  JSON.toJSONString(baseEntity);
    }

    /**
     * 删除商品信息
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteCommodity",method = RequestMethod.POST)
    private String deleteCommodity(HttpServletRequest request) {
        Integer code = -1;
        BaseEntity baseEntity =new BaseEntity();
        List idl =  new ArrayList();
        String ids = request.getParameter("ids");
        String idList[] = ids.split(",");
        for (String id : idList){
            idl.add(id);
        }
        if(commodityService.deleteCommodity(idl) > 0){
            code = 1;
        }
        baseEntity.setInfoCode(code);
        baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return  JSON.toJSONString(baseEntity);
    }


    /**
     * 修改商品
     * @param commodity
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateCommodity",method = RequestMethod.POST)
    private String updateCommodity(Commodity commodity,HttpServletRequest request) {
        Integer code = -1;
        BaseEntity baseEntity =new BaseEntity();
        if(commodityService.updateCommodity(commodity) > 0){
            code = 1;
        }
        baseEntity.setInfoCode(code);
        baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return  JSON.toJSONString(baseEntity);
    }


    /**
     * 查询商品列表
     * @param request
     * @return
     * @throws ParseException
     */
    @ResponseBody
    @RequestMapping(value = "/findCommodity",method = RequestMethod.GET)
    private String findCommodity(BaseEntity baseEntity,HttpServletRequest request) throws ParseException {
        return  JSON.toJSONString(commodityService.findCommodity(baseEntity));
    }


    /**
     * 兑换记录
     * @param baseEntity
     * @param request
     * @return
     * @throws ParseException
     */
    @ResponseBody
    @RequestMapping(value = "/queryExchangeRecord",method = RequestMethod.GET)
    private String queryExchangeRecord(BaseEntity baseEntity,HttpServletRequest request) throws ParseException {
        return  JSON.toJSONString(commodityService.queryExchangeRecord(baseEntity));
    }

    /***
     * 商品详情
     * @param request
     * @return
     * @throws ParseException
     */
    @ResponseBody
    @RequestMapping(value = "/findCommodityById",method = RequestMethod.GET)
    private String findCommodityById(HttpServletRequest request) throws ParseException {
        String id = request.getParameter("id");
        return  JSON.toJSONString(commodityService.findCommodityById(id));
    }


    /**
     * 查询我的兑换列表
     * @param request
     * @return
     * @throws ParseException
     */
    @ResponseBody
    @RequestMapping(value = "/findExchangeList",method = RequestMethod.GET)
    private String findExchangeList(HttpServletRequest request) throws ParseException {
        return  JSON.toJSONString(commodityService.findExchangeList(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId().toString()));
    }

    //查询我的兑换详情
    @ResponseBody
    @RequestMapping(value = "/findExchangeById",method = RequestMethod.GET)
    private String findExchangeById(HttpServletRequest request) throws ParseException {
        String id = request.getParameter("id");
        return  JSON.toJSONString(commodityService.findExchangeById(id));
    }

    /**
     * 兑换商品
     * @param exchange
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/exchangeCommodity",produces="text/plain;charset=UTF-8")
    private String exchangeCommodity(Exchange exchange, HttpServletRequest request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Integer code = -1;
        BaseEntity baseEntity =new BaseEntity();
        exchange.setType(1); //设置为兑换状态
        //人员id
        //String uid = userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId().toString();
        //String uid = "158";
        //带入小程序码参数   人员id，商品id
        //String scenes = uid+","+exchange.getCommodityId();
        //String scenes = uid+","+"345";
        //生成小程序码
        //String commodityCodeUrl = Wxacode.getCommodityWxCode(appid,secret,scenes,imageRootPath,realPath);  //地址
        //exchange.setCommodityCodeUrl("http://www.dichanjihe.cn/images/2.jpg");
        exchange.setAddTime(sdf.format(new Date()));
        synchronized (this) {
            //销库存
            Integer stock = commodityRepository.findNumById(exchange.getCommodityId()); //获取库存
            Integer num = commodityRepository.findNum(exchange.getCommodityId()); //获取兑换数
            num = Integer.valueOf(exchange.getNum()) + num;   //已兑换数
            Integer zs = stock - Integer.valueOf(exchange.getNum());  //商品库存
            Commodity commodity = new Commodity();
            commodity.setNum(num.toString());
            commodity.setStock(zs);
            commodity.setId(exchange.getCommodityId());
            if (commodityService.exchangeCommodity(exchange) > 0) {
                //2020-01-13 修改积分兑换规则
                commodityRepository.updateCommodity(commodity); //销库存
                Integer ujf = userRepository.getIntegral(exchange.getUserId()); //获取用户积分
                User user = new User();
                user.setId(exchange.getUserId());
                user.setIntegral(ujf - exchange.getIntegral() * Integer.valueOf(exchange.getNum()));   //目前没有收到前端多兑换的信息
                userRepository.updateUserJurisdiction(user);   //销积分
                code = 1;
            }
        }
        baseEntity.setInfoCode(code);
        baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return  JSON.toJSONString(baseEntity);
    }


    /**
     * 确定兑换
     * @param exchange
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/transactionCommodity",method = RequestMethod.POST)
    private String transactionCommodity(Exchange exchange,HttpServletRequest request) {
        Integer code = -1;
        BaseEntity baseEntity =new BaseEntity();
            //确定交易
            exchange.setType(2);
            if(commodityService.transactionCommodity(exchange) > 0){
                code = 1;
            }
        /*}*/
        baseEntity.setInfoCode(code);
        baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return  JSON.toJSONString(baseEntity);
    }

    //获取用户积分
    @ResponseBody
    @RequestMapping(value = "/getUserIntegral",method = RequestMethod.GET)
    private String getUserIntegral(HttpServletRequest request) throws ParseException {
        return  JSON.toJSONString(commodityService.getUserIntegral(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId().toString()));
    }


    /**
     * 生成对外优惠劵
     * @param coupon
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/insertWCoupon",method = RequestMethod.POST)
    private String insertWCoupon(Coupon coupon, HttpServletRequest request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer code = -1;
        BaseEntity baseEntity =new BaseEntity();
        coupon.setInfoTime(sdf.format(new Date()));
        if(commodityService.insertWCoupon(coupon) > 0){
            code = 1;
        }
        baseEntity.setInfoCode(code);
        baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return  JSON.toJSONString(baseEntity);
    }

    /**
     * 查询分配优惠劵分发人员列表
     * @param request
     * @return
     * @throws ParseException
     */
    @ResponseBody
    @RequestMapping(value = "/getUsrList",method = RequestMethod.GET)
    private String getUsrList(HttpServletRequest request) throws ParseException {
        List<User> lst = commodityService.getUsrList();
        return  JSON.toJSONString(lst);
    }

    /**
     * 生成对内兑换码
     * @param exchangeCoupon
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/insertNCoupon",method = RequestMethod.POST)
    private String insertNCoupon(ExchangeCoupon exchangeCoupon, HttpServletRequest request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer code = -1;
        BaseEntity baseEntity =new BaseEntity();
        Integer userId = null;
        Integer num = null;
        List uidList = new ArrayList();
        List numList = new ArrayList();
        List pUisList = new ArrayList();
        List pNumList = new ArrayList();
        //存操作员的优惠劵
        Integer length = exchangeCoupon.getLst().size();
        if (length >= 2){
            for (int i = 0;i<exchangeCoupon.getLst().size(); i++){
                if (i%2 ==0){
                    uidList.add(Integer.valueOf(exchangeCoupon.getLst().get(i).toString()));
                }else{
                    numList.add(Integer.valueOf(exchangeCoupon.getLst().get(i).toString()));
                }
            }
        }
        if (uidList.size() > 0) {
            for (int j = 0; j < uidList.size(); j++) {   //循环增加优惠劵
                for (int k = 0; k < Integer.valueOf(numList.get(j).toString()); k++) {  //循环优惠码张数
                    ExchangeCoupon ec = new ExchangeCoupon();
                    ec.setUserId(uidList.get(j).toString());
                    ec.setCode(IdGen.uuid());
                    ec.setCouponValue(exchangeCoupon.getCouponValue());
                    ec.setStartTime(exchangeCoupon.getStartTime());
                    ec.setEndTime(exchangeCoupon.getEndTime());
                    ec.setType(exchangeCoupon.getType());
                    ec.setUseType(0);
                    ec.setInfoTime(sdf.format(new Date()));
                    ec.setPropertyTye(exchangeCoupon.getPropertyTye());
                    ec.setDistributeType(1);
                    if (commodityService.insertNCoupon(ec)) {
                        code = 1;
                    }
                }
            }
        }
        //号码生成劵
        List pLength = exchangeCoupon.getpLst();
        Integer pl = 0;
        if (pLength != null){
            pl = pLength.size();
        }
        if (pl >= 2){
            for (int i = 0;i<exchangeCoupon.getpLst().size(); i++){
                if (i%2 ==0){
                    pUisList.add(Integer.valueOf(exchangeCoupon.getLst().get(i).toString()));
                }else{
                    pNumList.add(Integer.valueOf(exchangeCoupon.getLst().get(i).toString()));
                }
            }
        }
        if (pl > 0) {
            for (int jj = 0; jj < pUisList.size(); jj++) {   //循环增加优惠劵
                for (int kk = 0; kk < Integer.valueOf(pNumList.get(jj).toString()); kk++) {  //循环优惠码张数
                    ExchangeCoupon pec = new ExchangeCoupon();
                    pec.setPhone(pUisList.get(jj).toString());
                    pec.setCode(IdGen.uuid());
                    pec.setUseType(0);
                    pec.setCouponValue(exchangeCoupon.getCouponValue());
                    pec.setStartTime(exchangeCoupon.getStartTime());
                    pec.setEndTime(exchangeCoupon.getEndTime());
                    pec.setType(exchangeCoupon.getType());
                    pec.setPropertyTye(exchangeCoupon.getPropertyTye());
                    pec.setInfoTime(sdf.format(new Date()));
                    pec.setDistributeType(1);
                    if (commodityService.insertNCoupon(pec)) {
                        code = 1;
                    }
                }
            }
        }
        baseEntity.setInfoCode(code);
        baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return  JSON.toJSONString(baseEntity);
    }


    /**
     * 删除优惠劵
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteCoupon",method = RequestMethod.POST)
    private String deleteCoupon(HttpServletRequest request) {
        Integer code = -1;
        BaseEntity baseEntity =new BaseEntity();
        List idl =  new ArrayList();
        String ids = request.getParameter("ids");
        String idList[] = ids.split(",");
        for (String id : idList){
            idl.add(id);
        }
       // if(commodityService.deleteCoupon(idl) > 0){
        if(commodityService.deleteExchangeCoupon(idl) > 0){ //删除已经分发出去的优惠劵信息
            code = 1;
        }
        baseEntity.setInfoCode(code);
        baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return  JSON.toJSONString(baseEntity);
    }


    /**
     * 领取对外优惠劵
     * @param exchangeCoupon
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/receiveCoupon",method = RequestMethod.POST)
    private String receiveCoupon(ExchangeCoupon exchangeCoupon, HttpServletRequest request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer code = -1;
        BaseEntity baseEntity =new BaseEntity();
        synchronized (this) {
            Integer uid = userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId();
            Integer count = commodityService.countNum(uid,exchangeCoupon.getCouponId());
            if (count >= 1){
                code = -3397;
                baseEntity.setInfoCode(code);
                baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
                return  JSON.toJSONString(baseEntity);
            }
            Coupon num = commodityService.findCouponByIdDetails(exchangeCoupon.getCouponId().toString());
            if (num != null) {
                Integer s = num.getNum() - 1;
                if (s < 0) {
                    code = -3396;
                } else {
                    Coupon c = new Coupon();
                    c.setNum(s);
                    c.setId(exchangeCoupon.getCouponId());
                    if (commodityService.consumeCoupon(c) > 0) {   //减优惠劵数量
                        exchangeCoupon.setUseType(0);
                        exchangeCoupon.setType(0);
                        exchangeCoupon.setInfoTime(sdf.format(new Date()));
                        exchangeCoupon.setCouponValue(num.getCouponValue());
                        exchangeCoupon.setStartTime(num.getStartTime());
                        exchangeCoupon.setEndTime(num.getEndTime());
                        exchangeCoupon.setDistributeType(0);
                        exchangeCoupon.setPropertyTye(num.getPropertyTye());
                        if (commodityService.exchangeCoupon(exchangeCoupon) > 0) {   //领取优惠劵
                            code = 1;
                        }
                    }
                }
            }
        }
        baseEntity.setInfoCode(code);
        baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return  JSON.toJSONString(baseEntity);
    }


    /**
     * 兑换码使用
     * @param exchangeCoupon
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/useCode",method = RequestMethod.POST)
    private String useCode(ExchangeCoupon exchangeCoupon,HttpServletRequest request) {
        Integer code = 1;
        BaseEntity baseEntity =new BaseEntity();
        //获取
        exchangeCoupon.setUserId(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId().toString());
        exchangeCoupon.setDistributeType(0);
        if(commodityService.useCode(exchangeCoupon) > 0){
            code = 1;
        }
        baseEntity.setInfoCode(code);
        baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return  JSON.toJSONString(baseEntity);
    }


    /**
     * 查询对外优惠劵
     * @param request
     * @return
     * @throws ParseException
     */
    @ResponseBody
    @RequestMapping(value = "/queryWCoupon",method = RequestMethod.GET)
    private String queryWCoupon(HttpServletRequest request) throws ParseException {
        List<Coupon> lst = commodityService.queryWCoupon();
        return  JSON.toJSONString(lst);
    }

    /**
     * 个人查询优惠劵
     * @param request
     * @return
     * @throws ParseException
     */
    @ResponseBody
    @RequestMapping(value = "/getMyCoupon",method = RequestMethod.GET)
    private String getMyCoupon(HttpServletRequest request) throws ParseException {
        List<ExchangeCoupon> lst = commodityService.getMyCoupon(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId().toString(),userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getTelephone().toString());
        return  JSON.toJSONString(lst);
    }

    /**
     * 个人兑换码
     * @param request
     * @return
     * @throws ParseException
     */
    @ResponseBody
    @RequestMapping(value = "/getMyDCoupon",method = RequestMethod.GET)
    private String getMyDNCoupon(HttpServletRequest request) throws ParseException {
        List<ExchangeCoupon> lst = commodityService.getMyDNCoupon(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId().toString(),userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getTelephone().toString());
        return  JSON.toJSONString(lst);
    }

    /**
     * 查询所有优惠劵
     * @param request
     * @return
     * @throws ParseException
     */
    @ResponseBody
    @RequestMapping(value = "/findCouponAll",method = RequestMethod.GET)
    private String findCouponAll(BaseEntity baseEntity,HttpServletRequest request) throws ParseException {
        String scanName = request.getParameter("scanName"); //核销人
        String userName = request.getParameter("userName"); //使用人
        String houseCode = request.getParameter("houseCode"); //房号
        return  JSON.toJSONString(commodityService.findPagedByWhereClause(scanName,userName,houseCode,baseEntity));
    }

    /**
     * 查询优惠劵及兑换码详情
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findCouponByIdDetail",method = RequestMethod.GET)
    private String findCouponByIdDetail(HttpServletRequest request){
        String id = request.getParameter("id");
        return JSON.toJSONString(commodityService.findCouponByIdDetail(id));
    }


    //扫码进行用途填写
    @ResponseBody
    @RequestMapping(value = "/purpose",method = RequestMethod.POST)
    private String purpose(ExchangeCoupon exchangeCoupon,HttpServletRequest request) {
        Integer code = -1;
        BaseEntity baseEntity =new BaseEntity();
        if(commodityService.purpose(exchangeCoupon) > 0){
            code = 1;
        }
        baseEntity.setInfoCode(code);
        baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return  JSON.toJSONString(baseEntity);
    }


    /**
     * 查询优惠劵规则信息
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findCouponDetail",method = RequestMethod.GET)
    private String findCouponDetail(HttpServletRequest request){
        return JSON.toJSONString(commodityService.findCouponDetail());
    }


    /**
     * 修改优惠劵规则
     * @param couponEntity
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateCouponDetail",method = RequestMethod.POST)
    private String updateCouponDetail(CouponEntity couponEntity, HttpServletRequest request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer code = -1;
        BaseEntity baseEntity =new BaseEntity();
        CouponDetail couponDetail = new CouponDetail();
        couponDetail.setId(couponEntity.getId());
        couponDetail.setCouponDetail(couponEntity.getCouponDetail());
        couponDetail.setUpdateDate(sdf.format(new Date()));
        couponDetail.setUserId(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId());
        if(commodityService.updateCouponDetail(couponDetail) > 0){
            code = 1;
        }
        baseEntity.setInfoCode(code);
        baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return  JSON.toJSONString(baseEntity);
    }


    /**
     * 优惠劵扫码-核销
     * @param exchangeCoupon
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/verificationCoupon",method = RequestMethod.POST)
    private String verificationCoupon(ExchangeCoupon exchangeCoupon, HttpServletRequest request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Integer code = -1;
        BaseEntity baseEntity =new BaseEntity();
        //查询是否已售和销控
        //if ()
        exchangeCoupon.setExchangeTime(sdf.format(new Date()));
        exchangeCoupon.setScanName(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getName());
        exchangeCoupon.setScanId(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId());
        exchangeCoupon.setUseType(1);
        if(commodityService.verificationCoupon(exchangeCoupon) > 0){
            code = 1;
        }
        baseEntity.setInfoCode(code);
        baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return  JSON.toJSONString(baseEntity);
    }


    //扫码触发物业类型下拉加载
    @ResponseBody
    @RequestMapping(value = "/findPropertyTyeList",method = RequestMethod.GET)
    private String findPropertyTyeList(HttpServletRequest request){
        String propertyTye = request.getParameter("propertyTye");
        return JSON.toJSONString(commodityService.findCouponDetail());
    }

    //所有优惠劵下面的统计
    @ResponseBody
    @RequestMapping(value = "/getCouponTotal",method = RequestMethod.GET)
    private String getCouponTotal(HttpServletRequest request){
        return JSON.toJSONString(commodityService.getCouponTotal());
    }


    /**
     * 根据id查询用户基本信息
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getUserById",method = RequestMethod.GET)
    private String getUserById(HttpServletRequest request){
        String id = request.getParameter("id");
        return JSON.toJSONString(commodityService.getUserById(Integer.valueOf(id)));
    }


    /**
     * 签到送积分
     * @param signIn
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/signInQ",method = RequestMethod.POST)
    private String signInQ(SignIn signIn, HttpServletRequest request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer code = -1;
        BaseEntity baseEntity =new BaseEntity();
        //判断是否重复签到
        Integer uid = userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId();   //获取用户id
        Integer count = commodityService.signInVerification(uid,2,sdf.format(new Date()));
        if (count >= 1){    //返回已经签到过信息
            code = -5001;
            baseEntity.setInfoCode(code);
            baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
            return  JSON.toJSONString(baseEntity);
        }
        signIn.setUserId(uid);
        signIn.setInfoTime(sdf.format(new Date()));
        signIn.setType(2);  //每日签到类型
        Integer integral = commodityService.getIntegral(2); //获取签到积分
        signIn.setIntegralValue(integral);
        if(commodityService.signIn(signIn) > 0){
            Integer userIntegral = commodityService.queryUserIntegral(uid);
            User u = new User();
            u.setIntegral(integral+userIntegral);
            u.setId(uid);
            commodityService.updateUserIntegral(u);
            code = 1;
        }
        baseEntity.setInfoCode(code);
        baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return  JSON.toJSONString(baseEntity);
    }


    /**
     * 转发资讯获取积分
     * @param signIn
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/signInZX",method = RequestMethod.POST)
    private String signInZX(SignIn signIn, HttpServletRequest request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer code = -1;
        BaseEntity baseEntity =new BaseEntity();
        //判断是否重复签到
        Integer uid = userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId();   //获取用户id
        Integer maxValue = commodityService.getMaxValue(0); //获取转发数
        Integer count = commodityService.signInVerification(uid,0,sdf.format(new Date()));
        if (maxValue <= count){    //返回已经签到过信息
            return null;
        }
        signIn.setUserId(uid);
        signIn.setInfoTime(sdf.format(new Date()));
        signIn.setType(0);  //每日转发资讯类型
        Integer integral = commodityService.getIntegral(0); //获取签到积分
        signIn.setIntegralValue(integral);
        if(commodityService.signIn(signIn) > 0){
            Integer userIntegral = commodityService.queryUserIntegral(uid);
            User u = new User();
            u.setIntegral(integral+userIntegral);
            u.setId(uid);
            commodityService.updateUserIntegral(u);
            code = 1;
        }
        baseEntity.setInfoCode(code);
        baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return  JSON.toJSONString(baseEntity);
    }


    /**
     * 转发海报获取积分
     * @param signIn
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/signInHB",method = RequestMethod.POST)
    private String signInHB(SignIn signIn, HttpServletRequest request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer code = -1;
        BaseEntity baseEntity =new BaseEntity();
        //判断是否重复签到
        Integer uid = userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId();   //获取用户id
        Integer maxValue = commodityService.getMaxValue(1); //获取转发数
        Integer count = commodityService.signInVerification(uid,1,sdf.format(new Date()));
        if (maxValue <= count){    //返回已经签到过信息
            return null;
        }
        signIn.setUserId(uid);
        signIn.setInfoTime(sdf.format(new Date()));
        signIn.setType(1);  //每日转发资讯类型
        Integer integral = commodityService.getIntegral(1); //获取签到积分
        signIn.setIntegralValue(integral);
        if(commodityService.signIn(signIn) > 0){
            Integer userIntegral = commodityService.queryUserIntegral(uid);
            User u = new User();
            u.setIntegral(integral+userIntegral);
            u.setId(uid);
            commodityService.updateUserIntegral(u);
            code = 1;
        }
        baseEntity.setInfoCode(code);
        baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return  JSON.toJSONString(baseEntity);
    }


    /**
     * 新增客户评论
     * @param customerEvaluation
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/insertCustomerEvaluation",method = RequestMethod.POST)
    private String insertCustomerEvaluation(CustomerEvaluation customerEvaluation, HttpServletRequest request) {
        Integer code = -1;
        BaseEntity baseEntity =new BaseEntity();
        Integer receptionId = userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId();
        if (customerEvaluation.getReceptionId().equals(receptionId)){   //用户不能给自己添加标签
            code = -5010;
            baseEntity.setInfoCode(code);
            baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
            return  JSON.toJSONString(baseEntity);
        }
        customerEvaluation.setNum(1);
        customerEvaluation.setStatus(1);
        //判断是否超过标签上限3次
        if (commodityService.sx(customerEvaluation.getCustomerId()) > 3){
            code = -5005;
            baseEntity.setInfoCode(code);
            baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
            return  JSON.toJSONString(baseEntity);
        }
        if(commodityService.insertCustomerEvaluation(customerEvaluation) > 0){
            code = 1;
        }
        baseEntity.setInfoCode(code);
        baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return  JSON.toJSONString(baseEntity);
    }


    /**
     * 获取标签及数量
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getCountLabel",method = RequestMethod.GET)
    private String getCountLabel(HttpServletRequest request){
        String uid = request.getParameter("uid");
        return JSON.toJSONString(commodityService.getCountLabel(Integer.valueOf(uid)));
    }

    /**
     * 获取自己选中的标签
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getXz",method = RequestMethod.GET)
    private String getXz(HttpServletRequest request){
        String rid = request.getParameter("rid"); //置业顾问id
        Integer cid = userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId();
        return JSON.toJSONString(commodityService.getXz(rid,cid));
    }


    /**
     * 客户点赞置业顾问标签
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/fabulous",method = RequestMethod.POST)
    private String fabulous(CustomerEvaluation customerEvaluation,HttpServletRequest request) {
        Integer code = -1;
        BaseEntity baseEntity =new BaseEntity();
        customerEvaluation.setCustomerId(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId());
        customerEvaluation.setStatus(1);
        if(commodityService.fabulous(customerEvaluation) > 0){
            code = 1;
        }
        baseEntity.setInfoCode(code);
        baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return  JSON.toJSONString(baseEntity);
    }


    /**
     * 置业顾问删除标签
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteCustomerEvaluation",method = RequestMethod.POST)
    private String deleteCustomerEvaluation(HttpServletRequest request) {
        Integer code = -1;
        BaseEntity baseEntity =new BaseEntity();
        String label = request.getParameter("label");
        Integer uid = userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId();
        if(commodityService.deleteCustomerEvaluation(label,uid) > 0){
            code = 1;
        }
        baseEntity.setInfoCode(code);
        baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return  JSON.toJSONString(baseEntity);
    }


    /**
     * 查询用户详情 zz前端  老是想着不验证用户是否登录 导致前面不能加/xx/getUserDetailById
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getUserDetailById",method = RequestMethod.GET)
    private String getUserDetailById(HttpServletRequest request){
        String uid = request.getParameter("uid");
        return  JSON.toJSONString(userService.findById(Integer.valueOf(uid)));
    }


    /**
     * 个人点赞数
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getFabulousNum",method = RequestMethod.GET)
    private String getFabulousNum(HttpServletRequest request){
        String uid = request.getParameter("uid");
        return  JSON.toJSONString(commodityService.getFabulousNum(uid));
    }

    /**
     * 客户点赞状态
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getFabulousType",method = RequestMethod.GET)
    private String getFabulousType(HttpServletRequest request){
        String rid = request.getParameter("rid");  //置业顾问id
        Integer cid = userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId();
        return  JSON.toJSONString(commodityService.getFabulousTypes(cid,rid));
    }


    /**
     * 点赞置业顾问
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/insertReceptionFabulous",method = RequestMethod.POST)
    private String insertReceptionFabulous(HttpServletRequest request) {
        Integer code = -1;
        BaseEntity baseEntity =new BaseEntity();
        String rid = request.getParameter("rid"); //置业顾问id
        Integer cid = userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId();
        if(commodityService.insertReceptionFabulous(cid,Integer.valueOf(rid)) > 0){
            code = -5006;
            baseEntity.setInfoCode(code);
            baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
            return  JSON.toJSONString(baseEntity);
        }
        if (commodityService.saveReceptionFabulous(cid,Integer.valueOf(rid)) > 0){
            code = 1;
        }
        baseEntity.setInfoCode(code);
        baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return  JSON.toJSONString(baseEntity);
    }

    /**
     * 获取职业顾问标签点赞人数
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getCountFabulous",method = RequestMethod.GET)
    private String getCountFabulous(HttpServletRequest request){
        String rid = request.getParameter("rid");
        return  JSON.toJSONString(commodityService.getCountFabulous(Integer.valueOf(rid)));
    }

    /**
     * 优惠劵兑换积分/积分券 1:1
     */
    @ResponseBody
    @RequestMapping(value = "/setIntegralTicket",method = RequestMethod.POST)
    private String setIntegralTicket(ExchangeCoupon exchangeCoupon,HttpServletRequest request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer code = -1;
        Integer couponId = 0;
        BaseEntity baseEntity =new BaseEntity();
        Integer length = exchangeCoupon.getLst().size();
        List uidList = new ArrayList();   //置业顾问id
            if (length >= 2){
                Coupon c = new Coupon();
                Integer uid = userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId();
                c.setInfoTime(sdf.format(new Date()));
                c.setStartTime(exchangeCoupon.getStartTime());
                c.setEndTime(exchangeCoupon.getEndTime());
                c.setUserId(exchangeCoupon.getUserId());
                c.setCouponValue(exchangeCoupon.getCouponValue());
                c.setDistributeType(2);
                couponId = commodityService.saveCoupon(c);
                for (int i = 0;i<exchangeCoupon.getLst().size(); i++){
                    if (i%2 ==0){
                        uidList.add(Integer.valueOf(exchangeCoupon.getLst().get(i).toString()));
                    }
                }
            }
        if (uidList.size() > 0 && couponId > 0) {
            for (int j = 0; j < uidList.size(); j++) {   //循环增加积分劵
                    ExchangeCoupon ec = new ExchangeCoupon();
                    ec.setUserId(uidList.get(j).toString());
                    ec.setCouponValue(exchangeCoupon.getCouponValue());
                    ec.setStartTime(exchangeCoupon.getStartTime());
                    ec.setEndTime(exchangeCoupon.getEndTime());
                    ec.setType(2);
                    ec.setUseType(0);
                    ec.setInfoTime(sdf.format(new Date()));
                    ec.setDistributeType(2);
                    ec.setCouponId(couponId);
                    if (commodityService.insertNCoupon(ec)) {
                        code = 1;
                    }
            }
        }
        baseEntity.setInfoCode(code);
        baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return  JSON.toJSONString(baseEntity);
    }


    /**
     * 获取我的积分优惠券
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getIntegralTicket",method = RequestMethod.GET)
    private String getIntegralTicket(HttpServletRequest request){
        Integer uid = userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId();
        return JSON.toJSONString(commodityService.getIntegralTicket(uid));
    }


    /**
     * 客户扫码获取积分
     * @param exchangeCoupon
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/scanIntegralTicket",method = RequestMethod.POST)
    private String scanIntegralTicket(ExchangeCoupon exchangeCoupon,HttpServletRequest request) {
        Integer code = -1;
        BaseEntity baseEntity =new BaseEntity();
        Integer uid = userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId();
        if (commodityService.useCoupon(uid,exchangeCoupon.getCouponId()) > 0){
            code = -5009;
            baseEntity.setInfoCode(code);
            baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
            return  JSON.toJSONString(baseEntity);
        }
        Tintegral t = new Tintegral();
        t.setCouponId(exchangeCoupon.getCouponId());
        t.setUserId(uid);
        if (commodityService.saveTintegral(t) > 0){
            //获取用户积分信息
            Integer integral = 0; //用户积分
            integral = commodityService.getIntegralById(uid) + exchangeCoupon.getCouponValue();
            User u = new User();
            u.setId(uid);
            u.setIntegral(integral);
            commodityService.updateUserIntegral(u);
        }
        baseEntity.setInfoCode(code);
        baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return  JSON.toJSONString(baseEntity);
    }


    /**
     * 分享客户是否成为分享人客户
     * @param subordinateEntity
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/subordinate",method = RequestMethod.POST)
    private String subordinate(SubordinateEntity subordinateEntity, HttpServletRequest request) {
        Integer code = -1;
        BaseEntity baseEntity =new BaseEntity();
        if(commodityService.subordinate(subordinateEntity) > 0){
            code = 1;
        }
        baseEntity.setInfoCode(code);
        baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return  JSON.toJSONString(baseEntity);
    }




    /**
     * 线程池
     *
     */
    @ResponseBody
    @RequestMapping(value = "/async",method = RequestMethod.GET)
    private int async(HttpServletRequest request){
        int a = 0;
        for (int i=0;i< 5;i++){
            a+=1;
        }
        asyncService.nExecuteAsync();
        return a;
    }

}
